package com.utaedang.jesseclub.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import com.utaedang.jesseclub.JesseclubDetailActivity;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.webkit.WebView;

public class JesseclubDetailHTMLParse
{
	String HT_HEAD_S  = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><html><head></head><body onload='JavaScript:scrollDown();' bgcolor='black'>";
	String HT_HEAD_SCR = "<SCRIPT language=\"JavaScript\"> function scrollDown(){window.scroll(0, document.body.offsetHeight); }</SCRIPT>";
	String HT_HEAD_E  = "</body></html>";
	
	private static final String TAG = JesseclubDetailActivity.class.getSimpleName();
	
	private String url;
	private Context context;
	private Handler handler;
	private ProgressDialog progressDialog;
	private Source source;
	private WebView web;
	
	public JesseclubDetailHTMLParse(Context context, Handler handler, WebView web)
	{
		this.context = context;
		this.handler = handler;
		this.web = web;
	}
	
	public void open(String param)
	{
		url = "http://www.jesseclub.com/bbs/"+param;
		
		try
		{
			process();
		}catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	private void process() throws IOException
	{
		final Handler mHandler = new Handler();
		new Thread()
		{

			@Override
			public void run()
			{
				BufferedReader br = null;
		        String line;
		        String content = "";
				URL nURL;
				try
				{
					Calendar c = Calendar.getInstance(); 
					
					nURL = new URL(url);
					mHandler.post(new Runnable(){

						public void run()
						{
							progressDialog = ProgressDialog.show(context, "", "데이터 로딩 중...");
						}
					});
					
					//web.loadDataWithBaseURL("", "", "text/html", "UTF-8", "");
					
					URLConnection uCon = nURL.openConnection();
					
				    br = new BufferedReader( new InputStreamReader( uCon.getInputStream(), "euc-kr" ));

				    while( (line = br.readLine()) != null){
				    	String rtn = getConvData(line);
				    	if(!rtn.equals("null") && !rtn.equals("") && rtn != null){
				    		content += rtn + "<br><hr color='#ffffffff' size=1 noshade>";
				    		Log.d(TAG, rtn);
				        }
				    }
				    
				    //Log.d(TAG,android.text.Html.fromHtml(rtn).toString());
				    
				    content = HT_HEAD_S 
				    		+ HT_HEAD_SCR
				    		+ "<font size=3 color='white'>"
				    		+ content
				    		+ "<p align='right'>" + c.get(Calendar.HOUR_OF_DAY) + "시"	+ c.get(Calendar.MINUTE) + "분" + c.get(Calendar.SECOND) + "초</p></font>"
							+ HT_HEAD_E;
					
			        //String result = EncodingUtils.getString(content.getBytes(),    0, content.getBytes().length, "utf-8");
			        
				    web.loadDataWithBaseURL("", content.replaceAll("\\+", " ").replaceAll("null",""), "text/html", "UTF-8", "");
				    
					mHandler.post(new Runnable()
					{
						public void run()
						{
							progressDialog.cancel();
							handler.sendEmptyMessage(0);
						}
					});
				}catch (MalformedURLException e)
				{
					progressDialog.cancel();
					handler.sendEmptyMessage(0);
				}
				catch (IOException e)
				{
					progressDialog.cancel();
					handler.sendEmptyMessage(0);
				}
				
			}
			
		}.start();
	}
	
	//HTML 제거
    public static String getConvData(String htmlData){
    	String return_val = htmlData.replaceAll("\\<.*?>",""); //android.text.Html.fromHtml(htmlData).toString();
    	return_val = return_val.trim(); 
    	
    	if(!return_val.equals("")){
        	if(return_val.substring(0, 1).equals("▶")){
        		if(return_val.indexOf("매수")>-1||return_val.indexOf("돌파")>-1){
        			//notiTitle = "매수/돌파";
        			return_val="<font color='orange'>"+return_val+"</font>";
        		}else if(return_val.indexOf("매도")>-1||return_val.indexOf("이탈")>-1){
        			return_val="<font color='skyblue'>"+return_val+"</font>";
        			//notiTitle = "매도/이탈";
        		}else{
        			return_val="";
        		}
        	}else{
        		return_val="";
        	}
        }else{
        	return_val="";
        }
        return(return_val);
    }
}
